//
//  SnapKit+Localization.swift
//  PMS
//
//  Created by 何海涛 on 16/6/29.
//  Copyright © 2016年 途家. All rights reserved.
//

import Foundation

public typealias LocalizationConstraint = [String:(_ make:ConstraintMaker)->Void]

// extension for snap kit to full custom localization constraints.
extension UIView {
    
    /// 注意闭包持有问题
    public func snp_updateConstraintsLocalization(_ closures: LocalizationConstraint)  {
        if let closure = CustomLocalizationHelper.findClosestValueForCurrentLanguage(closures) {
            self.snp.updateConstraints { (make) in
                closure(make)
            }
        }
    }
    
    /// 注意闭包持有问题
    public func snp_makeConstraintsLocalization(_ closures: LocalizationConstraint) {
        if let closure = CustomLocalizationHelper.findClosestValueForCurrentLanguage(closures) {
            self.snp.makeConstraints { (make) in
                closure(make)
            }
        }
    }
    
    /// 注意闭包持有问题
    public func snp_remakeConstraintsLocalization(_ closures: LocalizationConstraint) {
        if let closure = CustomLocalizationHelper.findClosestValueForCurrentLanguage(closures) {
            self.snp.remakeConstraints { (make) in
                closure(make)
            }
        }
    }
}

extension ConstraintMakerEditable {
    
    public func multipliedByLocalization(_ amounts: [String:ConstraintMultiplierTarget]) -> ConstraintMakerEditable {
        if let amount = CustomLocalizationHelper.findClosestValueForCurrentLanguage(amounts) {
            return self.multipliedBy(amount)
        }
        return self
    }
    
    public func dividedByLocalization(_ amounts: [String:ConstraintMultiplierTarget]) -> ConstraintMakerEditable {
        if let amount = CustomLocalizationHelper.findClosestValueForCurrentLanguage(amounts) {
            return self.dividedBy(amount)
        }
        return self
    }
    
    @discardableResult
    public func offsetLocalization(_ amounts: [String:ConstraintOffsetTarget]) -> ConstraintMakerEditable {
        if let amount = CustomLocalizationHelper.findClosestValueForCurrentLanguage(amounts) {
            return self.offset(amount)
        }
        return self
    }
    
    public func insetLocalization(_ amounts: [String:ConstraintInsetTarget]) -> ConstraintMakerEditable {
        if let amount = CustomLocalizationHelper.findClosestValueForCurrentLanguage(amounts) {
            return self.inset(amount)
        }
        return self
    }
}

extension ConstraintMakerRelatable {
    
    func equalToLocalization(_ others:[String:ConstraintRelatableTarget], _ file: String = #file, _ line: UInt = #line) -> ConstraintMakerEditable {
        if let other = CustomLocalizationHelper.findClosestValueForCurrentLanguage(others) {
            return self.equalTo(other, file, line)
        }
        return throwLocalizationExecutingError()
    }
    
    func lessThanOrEqualToLocalization(_ others:[String: ConstraintRelatableTarget], _ file: String = #file, _ line: UInt = #line) -> ConstraintMakerEditable {
        if let other = CustomLocalizationHelper.findClosestValueForCurrentLanguage(others) {
            return self.equalTo(other, file, line)
        }
        return throwLocalizationExecutingError()
    }
    
    func greaterThanOrEqualToLocalization(_ others:[String: ConstraintRelatableTarget], _ file: String = #file, _ line: UInt = #line) -> ConstraintMakerEditable {
        if let other = CustomLocalizationHelper.findClosestValueForCurrentLanguage(others) {
            return self.equalTo(other, file, line)
        }
        return throwLocalizationExecutingError()
    }
    
    fileprivate func throwLocalizationExecutingError() -> ConstraintMakerEditable {
        fatalError(String(format: "can not find an executable value for current language '%@'", CustomLocalizationHelper.currentLanguage ?? ""))
    }
}
